package cm367;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.faces.bean.ManagedBean;


@ManagedBean
public class Facility {
	private int idFacility = -1;
	private String FacilityCode = "";
	private String FacilityDesc = "";
	private String FacilityType = "Electronic Equipment";
	private String FacilityFormula = "";
	private double FacilityUnitPrice = 0.00;

	public Facility() {
	}
	
	public Facility(int idFacility, String facilityCode,
			String facilityDesc, String facilityType, String facilityFormula,
			double facilityUnitPrice) {
		
		super();
		this.idFacility = idFacility;
		FacilityCode = facilityCode;
		FacilityDesc = facilityDesc;
		FacilityType = facilityType;
		FacilityFormula = facilityFormula;
		FacilityUnitPrice = facilityUnitPrice;
	}

	public int getIdFacility() {
		return idFacility;
	}

	public void setIdFacility(int idFacility) {
		this.idFacility = idFacility;
	}

	public String getFacilityCode() {
		return FacilityCode;
	}

	public void setFacilityCode(String facilityCode) {
		FacilityCode = facilityCode;
	}

	public String getFacilityDesc() {
		return FacilityDesc;
	}

	public void setFacilityDesc(String facilityDesc) {
		FacilityDesc = facilityDesc;
	}

	public String getFacilityType() {
		return FacilityType;
	}

	public void setFacilityType(String facilityType) {
		FacilityType = facilityType;
	}

	public String getFacilityFormula() {
		return FacilityFormula;
	}

	public void setFacilityFormula(String facilityFormula) {
		FacilityFormula = facilityFormula;
	}

	public double getFacilityUnitPrice() {
		return FacilityUnitPrice;
	}

	public void setFacilityUnitPrice(double facilityUnitPrice) {
		FacilityUnitPrice = facilityUnitPrice;
	}

	public double Formula2Value(double qty)  {

		double rst = 0;
		char chr;
        
		String exp = FacilityFormula;
		exp = exp.replace("[piece]", String.valueOf(qty));
		exp = exp.replace("[unitprice]", String.valueOf(FacilityUnitPrice));
		for(int idx = 0; idx < exp.length(); idx++){
			chr = exp.charAt(idx);
			if("0123456789.+-*/".indexOf(chr) == -1) {
				rst = -999999;
			}
		}
		if(rst == 0) {
			for(int idx = 0; idx < exp.length(); idx++){
				chr = exp.charAt(idx);
				if("0123456789.".indexOf(chr) == -1) {
					rst = -1;
				}
			}
			if(rst != -1){
				try {
					rst = Double.parseDouble(exp);
				} catch (NumberFormatException ex) {
					rst = -999999;
				}
			} else {
				try {
					rst = arithmetic(exp);  
				} catch (ArithmeticException ex) {
					rst = -999999;
				}
			}
		}
		
		return rst;
	}
	
	public static double arithmetic(String exp){  
        String result = "-999999";
		try {
			result = parseExp(exp).replaceAll("[\\[\\]]", "");
		} catch (Exception e) {
			e.printStackTrace();
		}  
        return Double.parseDouble(result);  
    }  

    public static String parseExp(String expression) throws Exception {  
   
        expression=expression.replaceAll("\\s+", "").replaceAll("^\\((.+)\\)$", "$1");  
//        String checkExp="\\d";  
        String minExp="^((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))$";  

        if(expression.matches(minExp)){  
            String result=calculate(expression);  
              
            return Double.parseDouble(result)>=0?result:"["+result+"]";  
        }
        
        String noParentheses="^[^\\(\\)]+$";  
        String priorOperatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\*\\/]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";  
        String operatorExp="(((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\]))[\\+\\-]((\\d+(\\.\\d+)?)|(\\[\\-\\d+(\\.\\d+)?\\])))";  
        if(expression.matches(noParentheses)){  
        	try {
                Pattern patt=Pattern.compile(priorOperatorExp);  
                Matcher mat=patt.matcher(expression);  
                if(mat.find()){  
                	String tempMinExp=mat.group();  
                	expression=expression.replaceFirst(priorOperatorExp, parseExp(tempMinExp));  
                }else{  
                	patt=Pattern.compile(operatorExp);  
                	mat=patt.matcher(expression);  
                  
                	if(mat.find()){  
                		String tempMinExp=mat.group();  
                		expression=expression.replaceFirst(operatorExp, parseExp(tempMinExp));  
                	}  	
                }  
        	} catch (PatternSyntaxException ex) {
        		expression = "-999999";
        	}
            return parseExp(expression);  
        }  

        String minParentheses="\\([^\\(\\)]+\\)";  
        Pattern patt=Pattern.compile(minParentheses);  
        Matcher mat=patt.matcher(expression);  
        if(mat.find()){  
            String tempMinExp=mat.group();  
            expression=expression.replaceFirst(minParentheses, parseExp(tempMinExp));  
        }  
        return parseExp(expression);  
    }  

    public static String calculate(String exp){  
        exp=exp.replaceAll("[\\[\\]]", "");  
        String number[]=exp.replaceFirst("(\\d)[\\+\\-\\*\\/]", "$1,").split(",");  
        BigDecimal number1=new BigDecimal(number[0]);  
        BigDecimal number2=new BigDecimal(number[1]);  
        BigDecimal result=null;  
          
        String operator=exp.replaceFirst("^.*\\d([\\+\\-\\*\\/]).+$", "$1");  
        if("+".equals(operator)){  
            result=number1.add(number2);  
        }else if("-".equals(operator)){  
            result=number1.subtract(number2);  
        }else if("*".equals(operator)){  
            result=number1.multiply(number2);  
        }else if("/".equals(operator)){  
            result=number1.divide(number2);  
        }  
          
        return result!=null?result.toString():null;  
    }  
}


